package com.hbsc.utils;

/**
 * Created by xudong on 2017-11-02.
 */
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.net.InetSocketAddress;
import java.net.Proxy;
public class RestTemplateUtil {

    /**
     * 获取支持SSL的RestTemplate
     * @return org.springframework.web.client.RestTemplate
     */
    public static RestTemplate getRestTemplateSsl(){
        HttpClient httpClient;
        try {
            httpClient = new SSLClient();
        }catch(Exception e){
            System.out.println(e.getMessage());
            httpClient=new DefaultHttpClient();
        }
        HttpComponentsClientHttpRequestFactory httpFactory=new HttpComponentsClientHttpRequestFactory(httpClient);
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        InetSocketAddress address = new InetSocketAddress("192.168.0.33", 80);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
        factory.setProxy(proxy);

        RestTemplate template=new RestTemplate(httpFactory);
        template.setRequestFactory(factory);
        return template;
    }

    /**
     * 获取RestTemplate
     * @return org.springframework.web.client.RestTemplate
     */
    public static RestTemplate getRestTemplate(){
        RestTemplate template=new RestTemplate();
        return template;
    }
}